/**
 * 中介者模式
 *  经典场景：机场指挥塔指挥飞机航线，博彩公司
 */


 // 泡泡堂游戏
 // 两人游戏
 function Player(name) {
     this.name = name;
     this.enemy = null;
 }
 Player.prototype.win = function() {
     console.log(this.name + 'won')
 }
 Player.prototype.lose = function() {
     console.log(this.name + 'lost')
 }
 Player.prototype.die = function() {
     this.lose();
     this.enemy.win()
 }
 var player1 = new Player('A');
 var player2 = new Player('B');
 player1.enemy = player2;
 player2.enemy = player1;
//  player2.die()

 // 为游戏增多队伍：红蓝队
 var players = [];
 function Player1(name, teamColor) {
     this.name = name;
     this.partners = []; // 队友列表
     this.enemys = []; // 敌人列表
     this.teamColor = teamColor; // 队伍颜色
     this.state = 'live'; // 玩家状态
 }
 // 团队胜利
 Player1.prototype.win = function() {
     console.log(this.name + ':won' );
 }
 // 团队失败
 Player1.prototype.lose = function() {
     console.log(this.name + ':lost' )
 }
 // 玩家die: 若队友都死了，则团队失败，若还有队友，则继续
 Player1.prototype.die = function() {
    this.state = 'dead';
    var all_dead = true;
    for(let i = 0, partner; partner = this.partners[i++];) {
        if (partner.state === 'live') {
            all_dead = false;
            break;
        }
    }
    if (all_dead) {
        // 都die了，失败
        this.lose();
        // 通知队友失败
        for(let i = 0, partner; partner = this.partners[i++];) {
            partner.lose()
        }
        // 通知敌人胜利
        for (let i = 0, enemy; enemy = this.enemys[i++];) {
            enemy.win();
        }
    }
 }
 // 定义一个工厂创建玩家
 var playerFactory = function(name, teamColor) {
     var newPlayer = new Player1(name, teamColor);
     for (let i = 0, player; player = players[i++];) {
         if (player.teamColor === newPlayer.teamColor) {
             player.partners.push(newPlayer);
             newPlayer.partners.push(player)
         }else {
             player.enemys.push(newPlayer);
             newPlayer.enemys.push(player);
         }
     }
     players.push(newPlayer);
     return newPlayer;
 }
 // 红队
 var p1 = playerFactory('p1', 'red');
 var p2 = playerFactory('p2', 'red');
 var p3 = playerFactory('p3', 'red');
 var p4 = playerFactory('p4', 'red');
 // 蓝队
 var p5 = playerFactory('p5', 'blue');
 var p6 = playerFactory('p6', 'blue');
 var p7 = playerFactory('p7', 'blue');
 var p8 = playerFactory('p8', 'blue');

//  p1.die();
//  p2.die();
//  p3.die();
//  p4.die();

 // 目前存在的问题：玩家与其他玩家耦合，若玩家增多，掉线，换队，会有很多问题

 // 使用中介者模式: 将与其他玩家耦合的操作交给中介者来做，比如买股票，具体收益由第三方帮我们算
// 玩家
 function Player2(name, teamColor) {
     this.name = name;
     this.teamColor = teamColor;
     this.state = 'alive'
 }
 Player2.prototype.win = function() {
     console.log(this.name + ':win');
 }
 Player2.prototype.lose = function() {
     console.log(this.name + '：lost')
 }
 // 玩家死亡
 Player2.prototype.die = function() {
     this.state = 'dead';
     playerDirector.reciveMessage('playerDead', this);
 }
 // 移除玩家
 Player2.prototype.remove = function() {
     playerDirector.reciveMessage('removePlayer', this);
 }
 // 玩家换队
 Player2.prototype.changeTeam = function(color) {
     playerDirector.reciveMessage('changeTeam', this, color);
 }
 // 玩家创建工厂
 var playerFactory = function(name, teamColor) {
    var newPlayer = new Player2(name, teamColor);
    // 通知中介添加玩家
    playerDirector.reciveMessage('addPlayer', newPlayer);
    return newPlayer;
 }
 // 中介者
 var playerDirector = (function() {
     var players = {}, // 所有玩家
     operations = {};
     //新增一个玩家
     operations.addPlayer = function(player) {
         var teamColor = player.teamColor;
         (players[teamColor] || (players[teamColor] = [])).push(player);
     }
     // 移除一个玩家
     operations.removePlayer = function(player) {
         var teamColor = player.teamColor;
         players[teamColor] = (players[teamColor] || []).filter(item => item !== player);
     }
     // 玩家换队
     operations.changeTeam = function(player, newTeamColor) {
         operations.removePlayer(player); // 从原队伍移除
         player.teamColor = newTeamColor;
         operations.addPlayer(player)
     }
     // 玩家die：如果队伍全灭，则lost，否则继续
     operations.playerDead = function(player) {
         var teamColor = player.teamColor,
         team = players[teamColor]; // 本队
         var all_dead = true;
         for(var i = 0, player; player = team[i++];) {
             if (player.state !== 'dead') {
                 all_dead = false;
                 break;
             }
         }

         if (all_dead) {
             // 通知本队lose
             for (var i = 0,player; player = team[i++];) {
                 player.lose();
             }
             //其他玩家win:只要一个队伍阵亡就结束了
             for(var color in players) {
                 if(color !== teamColor) {
                     var team = players[color];
                     for (var i = 0,player; player = team[i++];) {
                        player.win();
                    }
                 }
             }
         }
     }

     var reciveMessage = function() {
         var message = Array.prototype.shift.call(arguments); // 获取第一个参数
          operations[message].apply(this, arguments);
     }
     return {
         reciveMessage
     }
 })();

 // 红队：
var player1 = playerFactory( '皮蛋', 'red' ),
player2 = playerFactory( '小乖', 'red' ), 
player3 = playerFactory( '宝宝', 'red' ), 
player4 = playerFactory( '小强', 'red' ); 
// 蓝队：
var player5 = playerFactory( '黑妞', 'blue' ), 
player6 = playerFactory( '葱头', 'blue' ), 
player7 = playerFactory( '胖墩', 'blue' ), 
player8 = playerFactory( '海盗', 'blue' ); 

// player1.die(); 
// player2.die(); 
// player3.die(); 
// player4.die();

// player1.remove(); 
// player2.remove(); 
// player3.die(); 
// player4.die();

player1.changeTeam( 'blue' ); 
player2.die(); 
player3.die(); 
player4.die();